import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

abstract class BlocMviViewModel<Event, State, Effect>
    extends Bloc<Event, State> {
  BlocMviViewModel(super.initialState) {
    subscribeEvents();
  }

  final _sideEffectController = StreamController<Effect>.broadcast();

  Stream<Effect> get sideEffects => _sideEffectController.stream;

  void sendEffect(Effect effect) {
    _sideEffectController.add(effect);
  }

  void sendEvent(Event event){
    add(event);
  }

  @protected
  void subscribeEvents();

  @override
  Future<void> close() async {
    _sideEffectController.close();
    return super.close();
  }
}

final class NoneEffect {}

abstract class BlocSimpleMviViewModel<Event, State>
    extends BlocMviViewModel<Event, State, NoneEffect> {
  BlocSimpleMviViewModel(super.initialState);
}
